package com.szp.spCloud.user.controller;

import com.szp.spCloud.common.service.CodeValidateService;
import com.szp.spCloud.user.service.UserService;
import com.szp.spCloud.common.pojo.R;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    UserService userService;
    @Reference
    CodeValidateService codeValidateService;

    @RequestMapping("/register/{email}/{password}/{code}")
    public R register(@PathVariable(value = "email") String email, @PathVariable(value = "password") String password,
                            @PathVariable(value = "code") String code, HttpServletResponse response) {
        int r = codeValidateService.validateCode(email, code);
        if(r == 1){
            return R.error("验证码错误");
        }else if(r == 2){
            return R.error("验证码已过期");
        }
        int res = userService.register(email, password, code);
        if (res == 0) {
            String tokenStr = userService.login(email, password);
            Cookie cookie = new Cookie("token", tokenStr);
            cookie.setHttpOnly(false);
            cookie.setPath("/");
            response.addCookie(cookie);

            Cookie emailCookie = new Cookie("email", email);
            emailCookie.setHttpOnly(false);
            emailCookie.setPath("/");
            response.addCookie(emailCookie);
            return R.ok("注册成功");
        }else if(res == 1){
            return R.error("该用户已注册");
        }else {
            return R.error("验证码错误");
        }
    }

    @RequestMapping("/isRegistered/{email}")
    public boolean isRegister(@PathVariable(value = "email") String email) {
        return userService.isRegistered(email);
    }

    @RequestMapping("/login/{email}/{password}")
    public R login(@PathVariable(value = "email") String email, @PathVariable(value = "password") String password, HttpServletResponse response) {
        String tokenStr = userService.login(email, password);
        if(null == tokenStr)
            return R.error("用户名或密码错误");
        Cookie cookie = new Cookie("token", tokenStr);
        cookie.setHttpOnly(false);
        cookie.setPath("/");
        response.addCookie(cookie);

        Cookie emailCookie = new Cookie("email", email);
        emailCookie.setHttpOnly(false);
        emailCookie.setPath("/");
        response.addCookie(emailCookie);
        R r = R.ok("登录成功");
        r.put("token", tokenStr);
        r.put("email", email);
        return r;
    }
//
//    @RequestMapping("/info/{token}")
//    public String login(@PathVariable(value = "token") String token) {
//        return userService.info(token);
//    }
}
